package com.gcong.controller;

import com.gcong.model.SexEnum;
import com.gcong.model.Student;
import com.gcong.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by 巩聪 on 2018/1/7.
 */
@RestController
@RequestMapping(value = "student")
public class StudentController {
    @Autowired
    StudentService service;

    /*增*/
    @PostMapping
    public Student add(@RequestParam("age") int age, @RequestParam("name") String name,
                              @RequestParam("sex") SexEnum sex, @RequestParam("classTeacherId") Long classTeacherId) {
        Student student = new Student();
        student.setAge(age);
        student.setName(name);
        student.setSex(sex);
        student.setClassTeacherId(classTeacherId);
        return service.addNewStudent(student);
    }
    /*删*/
    @DeleteMapping
    public void delete(@RequestParam("id") Long id)
    {
        service.deleteStudent(id);
    }
    /*改*/
    @PutMapping
    public Student update(@RequestParam("id") Long id, @RequestParam("classTeacherId") Long classTeacherId){
        Student student = new Student();
        student.setId(id);
        student.setClassTeacherId(classTeacherId);
        return service.updateStudent(student);
    }
    /*查：全部*/
    @GetMapping
    public List<Student> get()
    {
        return service.findAll();
    }
    /*查：根据名字查*/
    @GetMapping("/name")
    public List<Student> getByName(@RequestParam("name") String name)
    {
        return service.findByName(name);
    }
    /*查：根据id查----mybatis*/

    @GetMapping("/id")
    public Student getOne(@RequestParam("id") Long id)
    {
        return service.findById(id);

    }
}
